# Maintainer: Thomas Liske <thomas@fiasko-nw.net>
pkgname=pypy3
pkgver=7.3.9
_pkgver=${pkgver/_/}
pkgrel=0
pkgdesc="Fast, compliant alternative implementation of Python 3.8"
url="https://www.pypy.org/"
arch="x86 x86_64 aarch64 s390x"
license="MIT"
makedepends="
	bzip2-dev
	expat-dev
	gdbm-dev
	libffi-dev
	linux-headers
	ncurses-dev
	openssl-dev
	pypy
	readline-dev
	rsync
	sqlite-dev
	tk-dev
	util-linux-misc
	xz-dev
	zlib-dev
	"
[ "$CARCH" = "x86" ] && options="textrels" # on x86 has textrels
source="https://downloads.python.org/pypy/pypy3.8-v$_pkgver-src.tar.bz2
	0001-time.patch
	0002-extern-stdio.patch
	"
builddir="$srcdir/pypy3.8-v$_pkgver-src"
options="$options !check" # todo

build() {
	cd pypy/goal

	# translate with JIT
	[[ "$JOBS" -gt 16 ]] && JOBS=16
	pypy ../../rpython/bin/rpython --opt=jit --shared --make-jobs=${JOBS:-2} targetpypystandalone

	# compile binary modules
	PYTHONPATH=../.. ./pypy3-c ../../lib_pypy/pypy_tools/build_cffi_imports.py
}

check() {
	cd pypy/goal
	./pypy3-c -u ../test_all.py \
		../module/pypyjit/test_pypy_c
}

package() {
	install -Dm755 pypy/goal/pypy3-c "$pkgdir"/usr/lib/pypy/bin/pypy3-c
	install -Dm755 pypy/goal/libpypy3-c.so "$pkgdir"/usr/lib/pypy/bin/libpypy3-c.so

	rsync -r --exclude='__pycache__' --exclude='*.c' --exclude '*.o' lib_pypy/ "$pkgdir"/usr/lib/pypy/lib_pypy/

	rsync -r --exclude='__pycache__' --exclude='test' --exclude='tests' lib-python/ "$pkgdir"/usr/lib/pypy/lib-python/

	rsync -r --include='*.h' -f 'hide,! */' include/ "$pkgdir"/usr/lib/pypy/include/

	# Install symlinks
	mkdir -p "$pkgdir"/usr/bin "$pkgdir"/usr/lib
	ln -s ../lib/pypy/bin/pypy3-c "$pkgdir"/usr/bin/pypy3
	ln -s pypy/bin/libpypy3-c.so "$pkgdir"/usr/lib/libpypy3-c.so
}

sha512sums="
2d1f1c0eda08344332fb983a1cd4e76391eb95197519d151d28ff7e6d3a337bc584908eed523a2f26aee0f62e61650353fada1a0d96d57dd197b2f349e033609  pypy3.8-v7.3.9-src.tar.bz2
42abc8c67c249ff012010aec568579cd738ed2d74ffdf93755c9427117e85cd859390266c7431fcb4d2fcfd82b61d5ba0c58bd0ceda44e5d12bf22df5935bb24  0001-time.patch
b648204914072e74367b94065d65b423324bcbabc0e2199ad195ab9822ccc6c51ec552c8c17ed22c26caf73b04d16d6f4c3e14a8d1af0e378d76d6c3b587b784  0002-extern-stdio.patch
"
